mui.init()

var classify = new Vue({
	el: '#classify',
	data: {
		imgCommonUrl: common.imageUrl,
		//左侧类型
		leftclass: [],
		//左侧选中
		leftchecks: -1,
		//右侧分类
		rightclass: [],
		//右侧内容
		rightmain: [],
		//右侧选择
		rightcheck: 0,
		//遮罩层
		backdrop: false,
		pageNum: 1,
		loading: false,
		finished: false
	},
	mounted: function() {
		//	左侧分类
		common.ajax('/category/getList/0', {}, function(data) {
			//			console.log(data.data)
			if(data.code == 1) {
				for(var i = 0; i < data.data.list.length; i++) {
					if(data.data.list[i].parentId == 0) {
						data.data.list[i].open = false
						classify.leftclass.push(data.data.list[i])
					}
				}
				for(var j = 0; j < classify.leftclass.length; j++) {
					classify.leftclass[j].childrenType = []
					for(var i = 0; i < data.data.list.length; i++) {
						if(classify.leftclass[j].id == data.data.list[i].parentId && data.data.list[i].parentId != 0) {
							classify.leftclass[j].childrenType.push(data.data.list[i])
						}
					}
				}
				if(localStorage.getItem('userData')) {
					var collection = {
						grade: 1,
						id: 'collection',
						name: "我的收藏",
						parentId: 0,
						open: false
					}
					classify.leftclass.unshift(collection)
				}
				if(classify.leftclass[0].childrenType) {
					// 默认选中第一个
					classify.leftchecks = classify.leftclass[0].childrenType[0].id
				} else if(classify.leftclass[0].id == 'collection') {
					classify.leftclass[0].open = true
					reloads()
				}
			} else {
				if(data.resultMsg) {
					mui.toast(data.resultMsg)
				}
			}

		}, false, 'get')
		this.checkDivScroolTsop()
	},
	methods: {
		//	展开左侧分类
		showchildren: function(data) {
			if(data.id == "collection") {
				if(data.open == true) {
					return
				}
				for(var i = 0; i < classify.leftclass.length; i++) {
					classify.leftclass[i].open = false
				}
				data.open = true
				$(".right_main").css('top', 0)
				$('.right_main').scrollTop(0)
				classify.leftchecks = false
				classify.rightclass = []
				classify.rightmain =[]
				classify.pageNum = 1
				reloads()
			} else {
				if(data.open) {
					data.open = false
				} else {
					for(var i = 0; i < classify.leftclass.length; i++) {
						classify.leftclass[i].open = false
					}
					data.open = true
					//显示第一个
					var swith = true
					for(var c = 0; c < data.childrenType.length; c++) {
						if(classify.leftchecks == data.childrenType[c].id) {
							swith = false
						}
					}
					if(swith) {
						classify.leftchecks = data.childrenType[0].id
					}
				}
			}
		},
		//点击左侧分类
		childrenCheck: function(data) {
			if(classify.leftchecks == data.id) {
				return
			} else {
				classify.leftchecks = data.id
			}
		},
		//点击右侧分类
		rightChecks: function(data) {
			classify.rightcheck = data.id
			/*请求内容*/
			common.ajax('/category/check/' + classify.rightcheck, {}, function(data) {
				if(data.code == 1) {
					classify.rightmain = data.data.list
					classify.$nextTick(function() {
						$(".right_main").css('top', $('.shop_right_top').height())
						$('.right_main').scrollTop(0)
					})
				} else {
					if(data.resultMsg) {
						mui.toast(data.resultMsg)
					}
				}
			}, false, 'get')
		},
		//点击添加到购物车，英文购物车在底部wiev试图在mask页面
		addShopping: function(data) {
			//			console.log(data.prodId)
			mui.plusReady(function() {
				classify.backdrop = true
				var w = plus.webview.create('../mask/clsaddcart.html', '../mask/clsaddcart.html', {
					top: '40%',
					bottom: '0'
				}, {
					preload: data.prodId
				});
				w.show('slide-in-bottom'); // 显示窗口
			})
		},

		checkDivScroolTsop: function() {
			var el = this.$refs.rightMain

			var offsetHeight = el.offsetHeight;
			el.onscroll = function() {
				var scrollTop = el.scrollTop;
				var scrollHeight = el.scrollHeight;
				if((offsetHeight + scrollTop) - scrollHeight >= -1) {
					// 需要执行的代码
					if(!classify.loading && !classify.finished && classify.leftclass[0].open) {
						classify.loading = true
						reloads()
						console.log(4242)
					}

				}
			};
		},
		//打开详情
		openDetails: function(data) {
			common.open('../details/details.html', '../details/details.html', data.prodId)
		}

	},
	watch: {
		leftchecks: function(curVal, oldVal) {
			if(!curVal) {
				return
			}
			//				右侧分类
			common.ajax('/category/getList/' + curVal, {}, function(data) {
				if(data.code == 1) {
					//					console.log(data)
					if(data.data.list.length <= 0) {
						classify.rightclass = []
						classify.rightcheck = curVal
					} else {
						//有就渲染
						classify.rightclass = data.data.list
						classify.rightcheck = data.data.list[0].id
					}
					/*请求内容*/
					common.ajax('/category/check/' + classify.rightcheck, {}, function(data) {
						if(data.code == 1) {
							classify.rightmain = data.data.list
							classify.$nextTick(function() {
								$(".right_main").css('top', $('.shop_right_top').height())
								$('.right_main').scrollTop(0)
							})
						} else {
							if(data.resultMsg) {
								mui.toast(data.resultMsg)
							}
						}
					}, false, 'get')

				} else {
					if(data.resultMsg) {
						mui.toast(data.resultMsg)
					}
				}
			}, false, 'get')
		}
	}
})
//收藏下拉加载
function reloads() {
	common.ajax('/userCenter/collect', {
		curPageNO: classify.pageNum++,
	}, function(data) {
		if(data.code == 1) {
			classify.rightmain = classify.rightmain.concat(data.data.list)
			classify.finished = data.data.pageCount < classify.pageNum ? true : false
			setTimeout(function(){
				classify.loading = false;
			}, 500)
		} else {
			if(data.resultMsg) {
				mui.toast(data.resultMsg)
			}
		}
	}, false, 'get')
}

mui.plusReady(function() {
	//	var _self = plus.webview.currentWebview();
	//	_self.setStyle({
	//		bounce: "vertical",
	//		bounceBackground: "red",
	//	});
	$('.seach_input input').on("tap", function() {
		var wp = plus.webview.create('../seach/seach.html', '../seach/seach.html', {
			scrollIndicator: 'none',
			scalable: false,
			popGesture: 'none'
		}, {
			preate: true
		})
		wp.show('fade-in');
	})
})

window.addEventListener('hideMask', function() {
	classify.backdrop = false
});
var close = mui.back
mui.back = function() {
	if(plus.webview.getWebviewById('../mask/clsaddcart.html')) {
		classify.backdrop = false
		plus.webview.close('../mask/clsaddcart.html');
	} else {
		close()
	}
}

Vue.use(VueLazyload, {
	preLoad: 1.3,
	error: '../../images/noimg.jpg',
	loading: '../../images/noimg.jpg'
});
var _toast = false
mui.back = function() {
	if(!_toast || !_toast.isVisible()) {
		_toast = mui.toast('再按一次返回键退出', {
			type: 'div'
		});
	} else {
		var main = plus.android.runtimeMainActivity();
		main.moveTaskToBack(false);
	}
}